package com.example.springbasicstudy.AOPfailed;

import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <pre>
 * 功能描述：解决AOP失败的问题。（事务、异步任务都是基于AOP实现的）
 * </pre>
 *
 * @author: 李明杰
 * @date: 2023/3/9 12:55
 */
@Service
@Slf4j
public class AsyncServiceImpl implements AsyncService {
    @Resource
    @Lazy //fixme 必须加上
    AsyncService asyncService;

    @SneakyThrows(Exception.class) //用法举例
    public void mainMethod() {
        log.info("======mainMethod=======");
        asyncService.childMethod1();
        asyncService.childMethod2();
        Thread.sleep(1000);

    }

    @Async
    @SneakyThrows
    public void childMethod1() {
        Thread.sleep(1000);
        log.info("======childMethod1=======");
    }

    @Async
    @SneakyThrows
    public void childMethod2() {
        Thread.sleep(1000);
        log.info("======childMethod2=======");
    }
}
